package app.web.cartoon.report.year.tool;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.lang.BusinessDate;

/**
 * 动漫番数统计
 * @author JiaJ
 *
 */
public class CartoonFansReport {
	
	private Map<String,Integer> totalMap = new HashMap<String,Integer>();
	
	private void instData(){
		
		String sql = "select count(*) TOTAL_NUM,CAN_FANS from ${schema}.LEO_CAN_TIME group by CAN_FANS";
		
		DataTable dataTable = DBFactory.getActionDB().executeQueryTable(sql);
		while(dataTable.next()){
			this.totalMap.put(dataTable.getString("CAN_FANS"), dataTable.getInt("TOTAL_NUM"));
		}
		
	}
	
	private void insertData(){
		
		DBFactory.getActionDB().executeUpdate("delete from LEO_CAN_REPORT_FANS");
		
		Set<String> fans = this.totalMap.keySet();
		for(String fan:fans){
			
			String year = fan.substring(0,4);
			String month = fan.substring(4);
			String date = BusinessDate.getToday();
			int num = this.totalMap.get(fan);
			
			String sql = "insert into ${schema}.LEO_CAN_REPORT_FANS (CAN_FANS,CAN_YEAR,CAN_MONTH,CAN_DETAIL_NUM,CAN_REP_DATE,CAN_REP_OPER) "
					+ " values ('"+fan+"','"+year+"','"+month+"',"+num+",to_date('"+date+"','yyyy-mm-dd'),'9999') ";
			
			DBFactory.getActionDB().executeUpdate(sql);
			
		}
		
		DBFactory.getActionDB().commit();
	}
	
	public void inst(){
		this.instData();
		this.insertData();
		this.free();
	}
	
	public void free(){
		this.totalMap.clear();
	}
	
}
